package com.edgclub.edg.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.GuZhiApplication;
import com.edgclub.edg.R;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.result.UserData;
import com.edgclub.edg.utils.util.CryptUtil;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.zhzhinn.activity.ZZRegisterSelectIconActivity;

public class RegisterSetPassActivity extends GzBaseTooolBarActivity implements View.OnClickListener , VolleyListener{
    private EditText password1 , password2;
    private Button next;
    private String verifyCode;
    private String phone;
    private String type;
    private UserPrefUtils userPrefUtil;
    private ImageView  two;
    private boolean isfirst;
    private boolean isTouchOne;
    private LinearLayout traceroute_rootview;
    @Override
    public int getContentView() {
        return R.layout.activity_register1;
    }

    @Override
    public void setTitleBarView() {
        type = getIntent().getStringExtra("type");
        phone = getIntent().getStringExtra("phone");
        verifyCode = getIntent().getStringExtra("verifyCode");
        setCustomActionBar();
        if ("1".equals(type)){
            title.setText("重置密码");
        }
        else{
            title.setText("欢迎注册EDG俱乐部");
        }
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }

    @Override
    public void initView() {
        password1 = (EditText) findViewById(R.id.password1);
        password2 = (EditText) findViewById(R.id.password2);
        traceroute_rootview = (LinearLayout) findViewById(R.id.traceroute_rootview);
        traceroute_rootview.setOnClickListener(this);
        two = (ImageView) findViewById(R.id.two);
        password1.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        password1.setCursorVisible(true);
                            if(!isTouchOne) {
                                two.setBackgroundResource(R.drawable.anim);
                                AnimationDrawable animation = (AnimationDrawable) two.getBackground();
                                animation.start();
                                isTouchOne = true;
                            }
                    } else {
                        // 此处为失去焦点时的处理内容
                    }
                }
        });
//
        password2.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        password2.setCursorVisible(true);
                        if(!isTouchOne) {
                            two.setBackgroundResource(R.drawable.anim);
                            AnimationDrawable animation = (AnimationDrawable) two.getBackground();
                            animation.start();
                            isTouchOne = true;
                        }
                    } else {
                        // 此处为失去焦点时的处理内容
                    }
                }
        });
        password1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isfirst = true;
                password1.setCursorVisible(true);
                password1.setFocusable(true);
                password1.setFocusableInTouchMode(true);
                password1.requestFocus();

                return false;
            }
        });
        password2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isfirst = true;
                password2.setCursorVisible(true);
                password2.setFocusable(true);
                password2.setFocusableInTouchMode(true);
                password2.requestFocus();
                return false;
            }
        });
        next = (Button) findViewById(R.id.next);
        if ("1".equals(type)){
            next.setText("完成");
        }else{
            next.setText("下一步");
        }
        next.setOnClickListener(this);
    }

    @Override
    public void initData() {

        userPrefUtil = new UserPrefUtils(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.next:
                if (password1.getText().toString().length()<8||password1.getText().toString().length()>16){
                    Toast.makeText(RegisterSetPassActivity.this,"密码长度不够8位",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!password1.getText().toString().trim().equals(password2.getText().toString().trim())){
                    Toast.makeText(RegisterSetPassActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(password1.getText().toString().trim())||"".equals(password2.getText().toString().trim())){
                    Toast.makeText(RegisterSetPassActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }

                if ("1".equals(type)){
                    forgetpsw();
                }else {
                    setpass();
                }
                break;
            case R.id.traceroute_rootview:
                InputMethodManager imm = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                if(isTouchOne) {
                    two.setBackgroundResource(R.drawable.animback);
                    AnimationDrawable animation = (AnimationDrawable) two.getBackground();
                    animation.start();
                }
                password1.setFocusable(false);
                password2.setFocusable(false);
                isTouchOne=false;
                break;

            case R.id.main_back:
                finish();
        }
    }
    public void setpass() {
        String strpsw = password2.getText().toString().trim();
        String umeng_channel = GuZhiApplication.getInstance().getChannel();
        UserParam param = new UserParam();
        param.setVerifyCode(verifyCode);
        param.setPhone(phone);
        param.setPwd(CryptUtil.md5(strpsw));
        param.setChannel(umeng_channel);
        new UserApi(this).registerPhone(param,this);
    }
    public void forgetpsw(){
        String strpsw = password2.getText().toString().trim();
        UserParam param = new UserParam();
        param.setVerifyCode(verifyCode);
        param.setPhone(phone);
        param.setPassword(CryptUtil.md5(strpsw));
        new UserApi(this).forgetPsw(param,this);
    }

    @Override
    public void onStart(String tag) {
            showDialog(StringUtils.getString(R.string.get_login), false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        if (ApiConfig.APIURL.URL_REGISTER_PHONE.equals(tag)) {
            if (JSONHelper.isSuccess(response)) {
                UserData userData = JsonUtil.getMode(response, UserData.class);
                if (null != userData && null != userData.getData()) {
                    userData.getData().setPhone(phone);
                    userPrefUtil.setIsLogin(true);
                    userPrefUtil.saveUser(userData.getData());
                }
                userPrefUtil.savePSW(CryptUtil.md5(password2.getText().toString().trim()));
                userPrefUtil.saveName(phone);
                Intent intent = new Intent(this, ZZRegisterSelectIconActivity.class);
                startActivity(intent);
                finish();
            }
        } else if (ApiConfig.APIURL.URL_FORGET_PSW.equals(tag)) {
                if (JSONHelper.isSuccess(response)) {
                    UserData userData = JsonUtil.getMode(response, UserData.class);
                    if (null != userData && null != userData.getData()) {
                        userData.getData().setPhone(phone);
                        userPrefUtil.saveUser(userData.getData());
                    }
                    userPrefUtil.saveName(phone);
                    userPrefUtil.savePSW(CryptUtil.md5(password2.getText().toString().trim()));
                    Toast.makeText(this, "重置密码成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(this, GzLoginActivity.class);
                    startActivity(intent);
                    finish();
                }

            }
        }

        @Override
        public void onFaile (VolleyError volleyError, String tag){
                dismissDialog();
                ResultCode.toastVolleyError(RegisterSetPassActivity.this, volleyError);
        }

}
